'use strict';

const { logrotator } = require('../../../config/plugin');

const Service = require('egg').Service;

class CommentService extends Service {

  // 获取文章评论列表
  async getCommentlist(payload) {
    const { app } = this;
    const Op = app.Sequelize.Op;
    const { pageNum, pageSize, article_id, user_id, status, parentId, keywords, begin_pubdate, end_pubdate } = payload;
    const where = {};
    if (article_id) { where.article_id = article_id; }// 根据文章id查询
    if (user_id) { where.user_id = user_id; }// 根据评论用户id查询
    if (status) { where.status = status; }// 根据评论状态查询
    if (parentId) { where.parentId = parentId; }// 根据评论 或回复查询 ，评论文章为0
    if (keywords) { where.content = { [Op.like]: `%${keywords}%` }; }// 根据评论内容查询
    if (begin_pubdate && end_pubdate) { // 根据评论时间查询
      where.createdAt = { [Op.between]: [ begin_pubdate, end_pubdate ] };
    }// 查询这个字段-xx之间  }
    try {
      const offset = (pageNum - 1) * pageSize;
      const { count, rows } = await app.model.ArticleComment.findAndCountAll({
        where,
        order: [
          [ 'createdAt', 'desc' ],
        ],
        include: [
          {
            model: this.app.model.Article,
            // attributes: [ 'title','id','category_id' ], // 只显示哪些字段
            as: 'comment_article',
            include: [
              {
                model: this.app.model.ArticleCategory,
                as: 'category',
                attributes: [ 'name' ], // 只显示哪些字段
                // through: { attributes: [] }, // 排除中间表
              },
              {
                model: this.app.model.User,
                as: 'user',
                attributes: [ 'nickname','avatar' ], // 只显示哪些字段
              },
            ],
          },
          {
            model: this.app.model.User,
            attributes: [ 'nickname', 'avatar' ], // 只显示哪些字段
            as: 'comment_user',
          },
          {
            model: this.app.model.User,
            // attributes: [ 'nickname', 'avatar' ], // 只显示哪些字段
            as: 'reply_user',
          },
        ],
        // raw: true,
        offset,
        limit: pageSize * 1,

      });
      return {
        pageNum,
        pageSize,
        total: count,
        // list: this.ctx.helper.toTree(rows),
        list: rows,
      };
    } catch (error) {
      console.error(error);
      return null;
    }
  }

  // 评论文章 新增
  async createComment(payload) {
    const { app } = this;
		console.log('payload======',payload);
    const res = await app.model.ArticleComment.create(payload);
    if (res.dataValues) {
      // 评论成功后 文章评论总数自增加1
      await app.model.Article.increment('comment_count', { where: { id: payload.article_id } });
    }
    return res.dataValues;
  }

  // // 获取用户评论点赞的列表
  // async getUserCommentLikingList(payload) {
  //   const { app } = this;
  //   const Op = app.Sequelize.Op;
  //   const { pageNum, pageSize, comment_id, user_id, status, begin_pubdate, end_pubdate } = payload;
  //   const where = {};
  //   if (comment_id) { where.comment_id = comment_id; }// 根据评论id查询
  //   if (user_id) { where.user_id = user_id; }// 根据点赞用户id查询
  //   if (status) { where.status = status; }// 根据点赞状态查询
  //   if (begin_pubdate && end_pubdate) { // 根据时间查询
  //     where.createdAt = { [Op.between]: [ begin_pubdate, end_pubdate ] };
  //   }// 查询这个字段-xx之间  }
  //   try {
  //     const offset = (pageNum - 1) * pageSize;
  //     const { count, rows } = await app.model.ArticleCommentLiking.findAndCountAll({
  //       where,
  //       order: [
  //         [ 'createdAt', 'desc' ],
  //       ],
  //       offset,
  //       limit: pageSize * 1,

  //     });
  //     return {
  //       pageNum,
  //       pageSize,
  //       total: count,
  //       list: rows,
  //     };
  //   } catch (error) {
  //     console.error(error);
  //     return null;
  //   }
  // }

  // 点赞评论 新增
  async createCommentLiking(payload) {
    const { app, ctx } = this;
    const { comment_id } = payload;
    const user_id = ctx.state.user.id;
    const transaction = await app.model.transaction();
    const isData = await app.model.ArticleCommentLiking.findOne({ where: { user_id, comment_id } });
    if (isData) {
      const err = {};
      err.msg = '您已点过赞啦';
      return err;
    }
    const res = await app.model.ArticleCommentLiking.create({ user_id, comment_id }, transaction);
    if (res.dataValues) {
      // 点赞成功后 评论点赞总数自增加1
      await app.model.ArticleComment.increment('like_count', { where: { id: comment_id } });
      await transaction.commit();
      return res.dataValues;
    }
    await transaction.rollback();
    return false;
  }

  // 取消对评论点赞
  async destroyCommentLiking(comment_id) {
    const { app, ctx } = this;
    const user_id = ctx.state.user.id;
    const transaction = await app.model.transaction();
    const res = await app.model.ArticleCommentLiking.destroy({ where: { comment_id, user_id }, force: true, transaction });
    if (res === 1) {
      await app.model.ArticleComment.decrement('like_count', { where: { id: comment_id } });
      await transaction.commit();
      return true;
    }
    await transaction.rollback();
    return false;
  }

  // // 获取评论点赞列表 获取当前用户点赞过的列表【数组id】
  // async getCommentLikingList(payload) {
  //   const { app } = this;
  //   const Op = app.Sequelize.Op;
  //   console.log(payload);
  //   const { comment_id, user_id, status, begin_pubdate, end_pubdate } = payload;
  //   const where = {};
  //   if (comment_id) { where.comment_id = comment_id; }// 根据评论id查询
  //   if (user_id) { where.user_id = user_id; }// 根据点赞用户id查询
  //   if (status) { where.status = status; }// 根据点赞状态查询
  //   if (begin_pubdate && end_pubdate) { // 根据时间查询
  //     where.createdAt = { [Op.between]: [ begin_pubdate, end_pubdate ] };
  //   }// 查询这个字段-xx之间  }
  //   try {
  //     const all = await app.model.ArticleCommentLiking.findAll({
  //       where,
  //       order: [[ 'createdAt', 'desc' ]],
  //     });
  //     // 根据当前用户点赞过的列表  循环评论id到新数组
  //     const newList = all.map(item => {
  //       return item.comment_id;
  //     });
  //     return newList;
  //   } catch (error) {
  //     console.error(error);
  //     return null;
  //   }
  // }


}

module.exports = CommentService;
